<template>
	<!-- 手写弹框 - 确保苹果手机兼容性 -->
	<view v-if="show" class="sku-popup-overlay" :class="{ 'show': show }">
		<view class="ss-modal-box bg-white ss-flex-col">
		<view class="ss-modal-box bg-white ss-flex-col">
			<view class="modal-header ss-flex ss-col-center">
				<view class="header-left ss-m-r-30">
					<image class="sku-image" :src="sheep.$url.cdn(state.selectedSkuPrice.image || props.goodsInfo.image)"
						mode="aspectFill"></image>
				</view>
				<view class="header-right ss-flex-col ss-row-between ss-flex-1">
					<view class="goods-title ss-line-2">{{ props.goodsInfo.title }}</view>
					<view class="header-right-bottom ss-flex ss-col-center ss-row-between">
						<view class="ss-flex">
							<!-- 共富会员：显示会员价和原价（original_price） -->
							<view v-if="props.userInfo && props.userInfo.level == 2" class="price-text mr20">
								{{ props.goodsInfo.vip_price || goodsPrice.price }}
							</view>
							<view v-if="props.userInfo && props.userInfo.level == 2 && props.goodsInfo.vip_price && props.goodsInfo.original_price" class="origin-price-text">
								{{ props.goodsInfo.original_price }}
							</view>
							
							<!-- 优惠会员：显示会员价和原价（original_price） -->
							<view v-if="props.userInfo && props.userInfo.level == 1" class="price-text mr20">
								{{ props.goodsInfo.vip_price || goodsPrice.price }}
							</view>
							<view v-if="props.userInfo && props.userInfo.level == 1 && props.goodsInfo.vip_price && props.goodsInfo.original_price" class="origin-price-text">
								{{ props.goodsInfo.original_price }}
							</view>
							
							<!-- 注册会员：只显示一个价格 -->
							<view v-if="!props.userInfo || props.userInfo.level == 0" class="price-text">
								{{ goodsPrice.price }}
							</view>
							
							<view class="ss-flex">
								<view v-if="goodsPrice.score > 0" class="score-text ss-m-l-4">+</view>
								<image v-if="goodsPrice.score > 0"
									:src="sheep.$url.static('/assets/addons/shopro/uniapp/goods/score1.svg')"
									class="score-img"></image>
								<view v-if="goodsPrice.score > 0" class="score-text">
									{{ goodsPrice.score }} 
								</view>
							</view>
						</view>

						<view class="stock-text ss-m-l-20">
							{{
                state.selectedSkuPrice.stock
                  ? formatStock(props.goodsInfo.stock_show_type, state.selectedSkuPrice.stock)
                  : formatStock(props.goodsInfo.stock_show_type, props.goodsInfo.stock)
              }}
						</view>
					</view>
				</view>
				<!-- 关闭按钮 -->
				<view class="close-btn" @tap="emits('close')">
					<text class="close-icon">×</text>
				</view>
			</view>
			<view class="modal-content ss-flex-1">
				<scroll-view scroll-y="true" class="modal-content-scroll" @touchmove.stop>
					<view class="sku-item ss-m-b-20" v-for="sku1 in (props.goodsInfo.skus || [])" :key="sku1.id">
						<view class="label-text ss-m-b-20">{{ sku1.name }}</view>
						<view class="ss-flex ss-col-center ss-flex-wrap">
							<button class="ss-reset-button spec-btn" v-for="sku2 in sku1.children" :class="[
                  {
                    'ui-BG-Main-Gradient': state.currentSkuArray[sku2.parent_id] == sku2.id,
                  },
                  {
                    'disabled-btn': sku2.disabled == true,
                  },
                ]" :key="sku2.id" :disabled="sku2.disabled == true" @tap="onSelectSku(sku2.parent_id, sku2.id)">
								{{ sku2.name }}
							</button>
						</view>
					</view>
					<view class="buy-num-box ss-flex ss-col-center ss-row-between ss-m-b-40">
						<view class="label-text">购买数量</view>
						<su-number-box :min="1" :max="state.selectedSkuPrice.stock" :step="1"
							v-model="state.selectedSkuPrice.goods_num" @change="onNumberChange($event)"></su-number-box>
					</view>
				</scroll-view>
			</view>
			<view class="modal-footer border-top">
				<view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center" v-if="isScore">
					<button class="ss-reset-button score-btn ui-Shadow-Main" @tap="onBuy">立即兑换</button>
				</view>
				<view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center" v-else-if="isCategoryPage">
					<!-- 分类页：只显示加入购物车按钮，使用商品详情页面样式 -->
					<button class="ss-reset-button category-page-btn" @tap="onAddCart">加入购物车</button>
				</view>
				<view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center" v-else-if="isGift != 0">
					<!-- 送好友模式：显示加入购物车和立即购买两个按钮 -->
					<view class="btn-group ss-flex ss-col-center">
						<view class="add-btn-info" @tap="onAddCart">加入购物车</view>
						<button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="onBuy">
							{{ isGift == 1 ? '立即赠送' : '立即购买' }}
						</button>
					</view>
				</view>
				<view class="buy-box ss-flex ss-col-center ss-flex ss-col-center ss-row-center" v-else>
					<!-- 商品详情页：显示加入购物车和立即购买两个按钮 -->
					<view class="btn-group ss-flex ss-col-center">
						<view class="add-btn-info" @tap="onAddCart">加入购物车</view>
						<button class="ss-reset-button buy-btn ui-Shadow-Main" @tap="onBuy">立即购买</button>
					</view>
				</view>
			</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { computed, reactive, watch } from 'vue';
	import sheep from '@/sheep';
	import { formatStock, formatPrice } from '@/sheep/hooks/useGoods';
	import { isEmpty } from 'lodash';

	const emits = defineEmits(['change', 'addCart', 'buy', 'close']);
	const props = defineProps({
		goodsInfo: {
			type: Object,
			default: () => ({}),
		},
		show: {
			type: Boolean,
			default: false,
		},
		isScore: {
			type: Boolean,
			default: false,
		},
		isGift: {
			type: [Number, String],
			default: 0
		},
		userInfo:{
			type: Object,
			default () {},
		},
		isCategoryPage: {
			type: Boolean,
			default: false
		}
	});

	const state = reactive({
		selectedSkuPrice: {},
		currentSkuArray: [],
	});


	//输入框改变数量
	function onNumberChange(e) {
		if (e === 0) return;
		if (state.selectedSkuPrice.goods_num === e) return;
		state.selectedSkuPrice.goods_num = e;
	}

	// 初始化SKU选择
	function initSkuSelection() {
		if (props.goodsInfo.sku_prices && props.goodsInfo.sku_prices.length > 0) {
			// 如果没有规格选择（只有尺码）或者没有sku，使用第一个价格
			if (!props.goodsInfo.is_sku || !props.goodsInfo.skus || props.goodsInfo.skus.length === 0) {
				state.selectedSkuPrice = props.goodsInfo.sku_prices[0];
				// 确保有默认数量
				if (state.selectedSkuPrice && !state.selectedSkuPrice.goods_num) {
					state.selectedSkuPrice.goods_num = 1;
				}
				// 确保goods_id字段存在
				if (!state.selectedSkuPrice.goods_id) {
					state.selectedSkuPrice.goods_id = props.goodsInfo.id;
				}
			} else {
				// 有规格但未选择时，清空selectedSkuPrice
				state.selectedSkuPrice = {};
			}
		}
	}

	// 初始化调用
	initSkuSelection();

	// 监听商品信息变化，重新初始化
	watch(() => props.goodsInfo, (newVal) => {
		if (newVal && Object.keys(newVal).length > 0) {
			initSkuSelection();
		}
	}, { immediate: true, deep: true });



	// 可选规格
	const skuPrices = computed(() => {
		let skuPrices = props.goodsInfo.sku_prices || [];
		if (props.goodsInfo.is_sku && skuPrices.length > 0) {
			skuPrices.forEach((item) => {
				if (item.goods_sku_ids) {
					item.goods_sku_id_arr = item.goods_sku_ids.split(',');
				}
			});
		}
		return skuPrices;
	});

	watch(
		() => state.selectedSkuPrice,
		(newVal) => {
			emits('change', newVal);
		}, {
			immediate: true, // 立即执行
			deep: true, // 深度监听
		},
	);

	const goodsPrice = computed(() => {
		let price, score;
		if (!state.selectedSkuPrice || Object.keys(state.selectedSkuPrice).length === 0) {
			price = (props.goodsInfo.price && props.goodsInfo.price.length > 0) ? props.goodsInfo.price[0] : 0;
			score = props.goodsInfo.score || 0;
		} else {
			price = state.selectedSkuPrice.price;
			score = state.selectedSkuPrice.score || 0;
		}
		return { price, score };
	});

	function onAddCart() {
		// 检查是否有规格需要选择
		if (props.goodsInfo.skus && props.goodsInfo.skus.length > 0) {
			// 有规格时，检查是否选择了所有规格
			let chooseSkuId = [];
			state.currentSkuArray.forEach((sku) => {
				if (sku != '') {
					chooseSkuId.push(sku);
				}
			});

			// 分类页面：也要求选择所有规格（参考订单详情逻辑）
			if (props.isCategoryPage) {
				// 检查是否选择了所有规格
				if (chooseSkuId.length < props.goodsInfo.skus.length) {
					sheep.$helper.toast('请选择规格');
					return;
				}

				let newPrice = getCanUseSkuPrice();
				if (newPrice.length > 0) {
					// 使用第一个可用的SKU价格
					state.selectedSkuPrice = newPrice[0];
					if (!state.selectedSkuPrice.goods_num) {
						state.selectedSkuPrice.goods_num = 1;
					}
					// 确保goods_id字段存在
					if (!state.selectedSkuPrice.goods_id) {
						state.selectedSkuPrice.goods_id = props.goodsInfo.id;
					}
				} else {
					// 没有找到匹配的SKU价格
					sheep.$helper.toast('请选择规格');
					return;
				}
			} else {
				// 非分类页面：要求选择所有规格
				if (chooseSkuId.length < props.goodsInfo.skus.length) {
					sheep.$helper.toast('请选择规格');
					return;
				}

				// 检查选中的规格是否有对应的SKU价格
				if (!state.selectedSkuPrice || Object.keys(state.selectedSkuPrice).length === 0) {
					sheep.$helper.toast('请选择规格');
					return;
				}
			}
		} else {
			// 无规格时，直接使用默认SKU
			if (!state.selectedSkuPrice || !state.selectedSkuPrice.goods_id) {
				sheep.$helper.toast('请选择规格');
				return;
			}
		}

		// 检查库存
		if (state.selectedSkuPrice.stock <= 0) {
			sheep.$helper.toast('库存不足');
			return;
		}

	// 确保必要字段存在
	const skuData = {
		...state.selectedSkuPrice,
		goods_sku_price_id: state.selectedSkuPrice.id, // 确保有 goods_sku_price_id 字段
		goods_id: state.selectedSkuPrice.goods_id || props.goodsInfo.id, // 确保有 goods_id 字段
	};
	
	console.log('SKU组件 - addCart - selectedSkuPrice:', state.selectedSkuPrice);
	console.log('SKU组件 - addCart - 传递的数据:', skuData);
	
	// 传递选中的SKU价格信息
	emits('addCart', skuData);
	}

	function onBuy() {
		// 检查是否有规格需要选择
		if (props.goodsInfo.skus && props.goodsInfo.skus.length > 0) {
			// 有规格时，检查是否选择了所有规格
			let chooseSkuId = [];
			state.currentSkuArray.forEach((sku) => {
				if (sku != '') {
					chooseSkuId.push(sku);
				}
			});

			// 分类页面：也要求选择所有规格（参考订单详情逻辑）
			if (props.isCategoryPage) {
				// 检查是否选择了所有规格
				if (chooseSkuId.length < props.goodsInfo.skus.length) {
					sheep.$helper.toast('请选择规格');
					return;
				}

				let newPrice = getCanUseSkuPrice();
				if (newPrice.length > 0) {
					// 使用第一个可用的SKU价格
					state.selectedSkuPrice = newPrice[0];
					if (!state.selectedSkuPrice.goods_num) {
						state.selectedSkuPrice.goods_num = 1;
					}
					// 确保goods_id字段存在
					if (!state.selectedSkuPrice.goods_id) {
						state.selectedSkuPrice.goods_id = props.goodsInfo.id;
					}
				} else {
					// 没有找到匹配的SKU价格
					sheep.$helper.toast('请选择规格');
					return;
				}
			} else {
				// 非分类页面：要求选择所有规格
				if (chooseSkuId.length < props.goodsInfo.skus.length) {
					sheep.$helper.toast('请选择规格');
					return;
				}

				// 检查选中的规格是否有对应的SKU价格
				if (!state.selectedSkuPrice || Object.keys(state.selectedSkuPrice).length === 0) {
					sheep.$helper.toast('请选择规格');
					return;
				}
			}
		} else {
			// 无规格时，直接使用默认SKU
			if (!state.selectedSkuPrice || !state.selectedSkuPrice.goods_id) {
				sheep.$helper.toast('请选择规格');
				return;
			}
		}

		// 检查库存
		if (state.selectedSkuPrice.stock <= 0) {
			sheep.$helper.toast('库存不足');
			return;
		}

	// 确保必要字段存在
	const skuData = {
		...state.selectedSkuPrice,
		goods_sku_price_id: state.selectedSkuPrice.id, // 确保有 goods_sku_price_id 字段
		goods_id: state.selectedSkuPrice.goods_id || props.goodsInfo.id, // 确保有 goods_id 字段
	};
	
	console.log('SKU组件 - buy - selectedSkuPrice:', state.selectedSkuPrice);
	console.log('SKU组件 - buy - 传递的数据:', skuData);
	
	// 传递选中的SKU价格信息
	emits('buy', skuData);
	}
	function getSkuSelectTip() {
		return '请选择规格';
	}

	// 改变禁用状态
	function changeDisabled(isChecked = false, pid = 0, skuId = 0) {
		let newPrice = []; // 所有可以选择的 skuPrice

		if (isChecked) {
			// 选中规格
			// 当前点击选中规格下的 所有可用 skuPrice
			for (let price of skuPrices.value) {
				if (price.stock <= 0) {
					// this.goodsNum 不判断是否大于当前选择数量，在 uni-number-box 判断，并且 取 stock 和 goods_num 的小值
					continue;
				}
				if (price.goods_sku_id_arr.indexOf(skuId.toString()) >= 0) {
					newPrice.push(price);
				}
			}
		} else {
			// 取消选中
			// 当前所选规格下，所有可以选择的 skuPrice
			newPrice = getCanUseSkuPrice();
		}

		// 所有存在并且有库存未选择的规格项 的 子项 id
		let noChooseSkuIds = [];
		for (let price of newPrice) {
			noChooseSkuIds = noChooseSkuIds.concat(price.goods_sku_id_arr);
		}

		// 去重
		noChooseSkuIds = Array.from(new Set(noChooseSkuIds));

		if (isChecked) {
			// 去除当前选中的规格项
			let index = noChooseSkuIds.indexOf(skuId.toString());
			noChooseSkuIds.splice(index, 1);
		} else {
			// 循环去除当前已选择的规格项
			state.currentSkuArray.forEach((sku) => {
				if (sku.toString() != '') {
					// sku 为空是反选 填充的
					let index = noChooseSkuIds.indexOf(sku.toString());
					if (index >= 0) {
						// sku 存在于 noChooseSkuIds
						noChooseSkuIds.splice(index, 1);
					}
				}
			});
		}

		// 当前已选择的规格大类
		let chooseSkuKey = [];
		if (!isChecked) {
			// 当前已选择的规格大类
			state.currentSkuArray.forEach((sku, key) => {
				if (sku != '') {
					// sku 为空是反选 填充的
					chooseSkuKey.push(key);
				}
			});
		} else {
			// 当前点击选择的规格大类
			chooseSkuKey = [pid];
		}

		for (let i in props.goodsInfo.skus) {
			// 当前点击的规格，或者取消选择时候 已选中的规格 不进行处理
			if (chooseSkuKey.indexOf(props.goodsInfo.skus[i]['id']) >= 0) {
				continue;
			}

			for (let j in props.goodsInfo.skus[i]['children']) {
				// 如果当前规格项 id 不存在于有库存的规格项中，则禁用
				if (noChooseSkuIds.indexOf(props.goodsInfo.skus[i]['children'][j]['id'].toString()) >= 0) {
					props.goodsInfo.skus[i]['children'][j]['disabled'] = false;
				} else {
					props.goodsInfo.skus[i]['children'][j]['disabled'] = true;
				}
			}
		}
	}
	// 当前所选规格下，获取所有有库存的 skuPrice
	function getCanUseSkuPrice() {
		let newPrice = [];

		for (let price of skuPrices.value) {
			if (price.stock <= 0) {
				// 库存不足的跳过
				continue;
			}

			// 检查当前选择的规格是否都包含在这个 SKU 价格中
			let isMatch = true;

			// 遍历当前选择的规格
			for (let i = 0; i < state.currentSkuArray.length; i++) {
				const selectedSkuId = state.currentSkuArray[i];
				if (selectedSkuId && selectedSkuId != '') {
					// 如果这个 SKU 价格不包含当前选择的规格项，则不匹配
					if (price.goods_sku_id_arr.indexOf(selectedSkuId.toString()) < 0) {
						isMatch = false;
						break;
					}
				}
			}

			if (isMatch) {
				newPrice.push(price);
			}
		}

		return newPrice;
	}
	// 选择规格
	function onSelectSku(pid, skuId) {
		// 清空已选择
		let isChecked = true; // 选中 or 取消选中
		if (state.currentSkuArray[pid] != undefined && state.currentSkuArray[pid] == skuId) {
			// 点击已被选中的，删除并填充 ''
			isChecked = false;
			state.currentSkuArray.splice(pid, 1, '');
		} else {
			// 选中
			state.currentSkuArray[pid] = skuId;
		}

		let chooseSkuId = []; // 选中的规格大类
		state.currentSkuArray.forEach((sku) => {
			if (sku != '') {
				// sku 为空是反选 填充的
				chooseSkuId.push(sku);
			}
		});

		// 当前所选规格下，所有可以选择的 skuPric
		let newPrice = getCanUseSkuPrice();

		// 判断所有规格大类是否选择完成
		if (chooseSkuId.length == props.goodsInfo.skus.length && newPrice.length > 0) {
			// 规格选择完成，设置选中SKU
			newPrice[0].goods_num = state.selectedSkuPrice.goods_num || 1;
			state.selectedSkuPrice = newPrice[0];
			// 确保goods_id字段存在
			if (!state.selectedSkuPrice.goods_id) {
				state.selectedSkuPrice.goods_id = props.goodsInfo.id;
			}
		} else if (chooseSkuId.length < props.goodsInfo.skus.length) {
			// 规格未选择完成
			if (props.isCategoryPage) {
				// 分类页面：也要求选择所有规格，清空选中SKU
				state.selectedSkuPrice = {};
			} else {
				// 非分类页面：规格未选择完成，清空选中SKU
				state.selectedSkuPrice = {};
			}
		} else {
			// 规格数量匹配但没有可用的SKU价格
			state.selectedSkuPrice = {};
		}
	}

</script>

<style lang="scss" scoped>
	/* 手写弹框样式 - 确保苹果手机兼容性 */
	.sku-popup-overlay {
		position: fixed !important;
		top: 0 !important;
		left: 0 !important;
		right: 0 !important;
		bottom: 0 !important;
		width: 100vw !important;
		height: 100vh !important;
		background: rgba(0, 0, 0, 0);
		z-index: 999999 !important;
		display: flex !important;
		align-items: flex-end;
		justify-content: center;
		/* 苹果设备优化 */
		-webkit-overflow-scrolling: touch;
		overscroll-behavior: contain;
		transform: translateZ(0);
		-webkit-transform: translateZ(0);
		/* 动画效果 */
		transition: all 0.3s ease;
		opacity: 0;
		visibility: hidden;

		&.show {
			background: rgba(0, 0, 0, 0.6) !important;
			opacity: 1 !important;
			visibility: visible !important;
			display: flex !important;
			position: fixed !important;
			top: 0 !important;
			left: 0 !important;
			right: 0 !important;
			bottom: 0 !important;
			z-index: 999999 !important;
		}
	}

	.ss-modal-box {
		border-radius: 30rpx 30rpx 0 0;
		width: 100vw !important;
		max-width: 100% !important;
		/* 优化高度计算，确保在所有设备上都能正确显示，包括底部按钮 */
		max-height: calc(60vh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
		/* 添加苹果设备兼容性 */
		-webkit-overflow-scrolling: touch;
		/* 确保内容不会超出屏幕 */
		overflow-x: hidden;
		/* 确保弹框在所有设备上都有足够的空间显示按钮 */
		min-height: 600rpx;
		/* 使用 flexbox 布局确保内容正确分布 */
		display: flex !important;
		flex-direction: column;
		/* 添加底部安全距离，确保弹框不会贴边 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
		/* 为商品详情页弹框添加上边距，确保对齐 */
		/* 确保弹框在左侧分类菜单之上 */
		position: relative;
		z-index: 1000000 !important;
		/* 苹果设备硬件加速 */
		transform: translateZ(0);
		-webkit-transform: translateZ(0);
		/* 动画效果 - 从底部滑入 */
		transform: translateY(100%);
		-webkit-transform: translateY(100%);
		transition: transform 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);

		&.show {
			transform: translateY(0) !important;
			-webkit-transform: translateY(0) !important;
			display: flex !important;
			position: relative !important;
			z-index: 1000000 !important;
			opacity: 1 !important;
			visibility: visible !important;
		}
	}

	.mr20{
		margin-right:20rpx;
	}
	.ml20{
		margin-left:20rpx;
	}
	.origin-price-text {
		font-size: 26rpx;
		font-weight: 400;
		text-decoration: line-through;
		color: $gray-c;
		font-family: OPPOSANS;
	}
	// 购买
	.buy-box {
		padding: 10rpx 0;

		.add-btn {
			width: 356rpx;
			height: 80rpx;
			border-radius: 8rpx 0 0 40rpx;
			background-color: var(--ui-BG-Main-light);
			color: var(--ui-BG-Main);
		}
		.add-btn-info {
			width: 80%;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			background: rgba(255, 124, 0, 0.1);
			border-radius: 8rpx;
			font-weight: 400;
			font-size: 28rpx;
			color: #FF7C00;
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 0 auto;
			margin-top:10rpx;
		}

		.buy-btn-info {
			width: 45%;
			height: 88rpx;
			line-height: 88rpx;
			text-align: center;
			background: #FF7C00;
			border-radius: 5rpx 5rpx 5rpx 5rpx;
			border: 1rpx solid #FF7C00;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			font-size: 28rpx;
			color: #FFFFFF;
		}

		.buy-btn {
			width: 356rpx;
			height: 80rpx;
			border-radius: 8rpx;
			background: #FF7C00;
			color: #fff;
		}

		// 分类页面专用按钮样式 - 方形设计
		.category-page-btn {
			width: 100% !important;
			height: 80rpx !important;
			font-weight: 500;
			font-size: 28rpx;
			border-radius: 8rpx !important;
			background: linear-gradient(90deg, #FF7C00, #FF9500) !important;
			color: #fff !important;
			margin: 0 20rpx;
			display: flex !important;
			align-items: center !important;
			justify-content: center !important;
			border: none !important;
			box-shadow: 0 4rpx 12rpx rgba(255, 124, 0, 0.3);
			transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
			transform: scale(1);

			&:hover {
				box-shadow: 0 6rpx 16rpx rgba(255, 124, 0, 0.4);
			}

			&:active {
				box-shadow: 0 4rpx 12rpx rgba(255, 124, 0, 0.5);
			}
		}

		

		.score-btn {
			width: 100%;
			margin: 0 20rpx;
			height: 80rpx;
			border-radius: 8rpx;
			background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
			color: #fff;
		}
	}

	.ss-modal-box {
		border-radius: 30rpx 30rpx 0 0;
		width: 100vw !important;
		max-width: 100% !important;
		/* 优化高度计算，确保在所有设备上都能正确显示，包括底部按钮 */
		max-height: calc(95vh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
		/* 添加苹果设备兼容性 */
		-webkit-overflow-scrolling: touch;
		/* 确保内容不会超出屏幕 */
		overflow-x: hidden;
		/* 确保弹框在所有设备上都有足够的空间显示按钮 */
		min-height: 600rpx;
		/* 使用 flexbox 布局确保内容正确分布 */
		display: flex !important;
		flex-direction: column;
		/* 添加底部安全距离，确保弹框不会贴边 */
		padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);
		/* 为商品详情页弹框添加上边距，确保对齐 */
		/* 确保弹框在左侧分类菜单之上 */
		position: relative;
		z-index: 1000000 !important;
		/* 苹果设备硬件加速 */
		transform: translateZ(0);
		-webkit-transform: translateZ(0);

		.modal-header {
			position: relative;
			padding: 80rpx 20rpx 40rpx;

			.sku-image {
				width: 140rpx;
				height: 140rpx;
				border-radius: 10rpx;
			}

			.header-right {
				height: 140rpx;
			}

			.close-btn {
				position: absolute;
				top: 15rpx;
				right: 15rpx;
				width: 48rpx;
				height: 48rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				background: rgba(0, 0, 0, 0.1);
				border-radius: 50%;
				z-index: 10;
				transition: background-color 0.2s ease;
				opacity: 0.8;

				.close-icon {
					font-size: 32rpx;
					color: #666;
					font-weight: 300;
					line-height: 1;
					font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
				}

				&:hover {
					background: rgba(0, 0, 0, 0.15);
					opacity: 1;
				}

				&:active {
					background: rgba(0, 0, 0, 0.2);
				}
			}

			.goods-title {
				font-size: 28rpx;
				font-weight: 500;
				line-height: 42rpx;
			}

			.score-img {
				width: 36rpx;
				height: 36rpx;
				margin: 0 4rpx;
			}

			.score-text {
				font-size: 30rpx;
				font-weight: 500;
				color: $red;
				font-family: OPPOSANS;
			}

			.price-text {
				font-size: 30rpx;
				font-weight: 500;
				color: #FF7C00;
				font-family: OPPOSANS;

				&::before {
					content: '￥';
					font-size: 30rpx;
					font-weight: 500;
					color: #FF7C00;
				}
			}

			.stock-text {
				font-size: 26rpx;
				color: #999999;
			}
		}

		.modal-content {
			padding: 0 20rpx;
			/* 优化内容区域的高度计算 */
			flex: 1;
			min-height: 0;
			/* 确保内容区域有足够的空间，但不会挤压底部按钮 */
			max-height: calc(30vh - env(safe-area-inset-bottom));
			/* 使用 flexbox 布局 */
			display: flex;
			flex-direction: column;
			/* 添加底部边距，与按钮区域分开 */
			margin-bottom: 10rpx;

			.modal-content-scroll {
				/* 优化滚动区域高度，确保在所有设备上都能正确显示 */
				flex: 1;
				max-height: calc(25vh - env(safe-area-inset-bottom));
				/* 添加苹果设备兼容性 */
				-webkit-overflow-scrolling: touch;
				/* 添加底部边距，确保内容不会贴边 */
				padding-bottom: 10rpx;

				.label-text {
					font-size: 26rpx;
					font-weight: 500;
				}

				.buy-num-box {
					height: 100rpx;
				}

				.spec-btn {
					height: 60rpx;
					min-width: 100rpx;
					padding: 0 30rpx;
					background: #F8F8F8;
					border-radius: 8rpx;
					color: #434343;
					font-size: 26rpx;
					margin-right: 10rpx;
					margin-bottom: 10rpx;
					transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
					transform: scale(1);
					opacity: 1;

					&:hover {
						box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
					}

					&:active {
						box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.15);
					}
				}


				.spec-btn.ui-BG-Main-Gradient {
					background: #FF7C00 !important;
					color: #FFFFFF !important;
					border-color: #FF7C00 !important;
					font-weight: 500;
					box-shadow: 0 4rpx 12rpx rgba(255, 124, 0, 0.3);
					transition: all 0.2s ease;

					&:active {
						background: #FF6B00 !important;
						border-color: #FF6B00 !important;
						box-shadow: 0 2rpx 8rpx rgba(255, 124, 0, 0.4);
					}
				}

				.disabled-btn {
					font-weight: 400;
					color: #c6c6c6 !important;
					background: #f4f4f4 !important;
					border-radius: 8rpx !important;
					opacity: 0.6;
					transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);

					&:active {
						background: #f4f4f4 !important;
						color: #c6c6c6 !important;
						transform: scale(1);
					}
				}
			}
		}

		.modal-footer {
			padding: 15rpx;
			/* 确保底部按钮区域有足够的空间 */
			min-height: 90rpx;
			/* 添加顶部边距，与内容区域分开 */
			margin-top: 10rpx;
			/* 使用 flexbox 布局确保按钮正确显示 */
			display: flex;
			flex-direction: column;
			justify-content: center;
			/* 确保底部按钮区域不会被挤压 */
			flex-shrink: 0;


			.buy-box {
				/* 确保按钮容器有足够的空间 */
				min-height: 70rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				/* 确保按钮不会被挤压 */
				flex-shrink: 0;
				/* 添加底部边距，确保按钮与底部有适当距离 */
				margin-bottom: 10rpx;
				
				.btn-group {
					width: 100%;
					gap: 20rpx;
					/* 确保按钮组有足够的空间 */
					min-height: 70rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					/* 添加底部边距，确保按钮与底部有适当距离 */
					margin-bottom: 10rpx;
					/* 确保按钮组容器有正确的尺寸 */
					box-sizing: border-box;

					.add-btn-info {
						flex: 1;
						height: 80rpx;
						line-height: 80rpx;
						text-align: center;
						background: rgba(255, 124, 0, 0.1);
						border-radius: 8rpx;
						font-size: 28rpx;
						font-weight: 500;
						color: #FF7C00;
						border: 2rpx solid #FF7C00;
						/* 确保按钮完全显示 */
						display: flex;
						align-items: center;
						justify-content: center;
						/* 防止按钮被挤压 */
						flex-shrink: 0;
						/* 设置边框盒模型，确保边框不占用额外空间 */
						box-sizing: border-box;
						/* 动画效果 */
						transition: all 0.2s ease;

						&:active {
							background: rgba(255, 124, 0, 0.15);
							box-shadow: 0 2rpx 8rpx rgba(255, 124, 0, 0.2);
						}
					}

					.buy-btn {
						flex: 1;
						height: 80rpx;
						line-height: 80rpx;
						text-align: center;
						background: linear-gradient(90deg, #FF7C00, #FF9500);
						border-radius: 8rpx;
						font-size: 28rpx;
						font-weight: 500;
						color: #FFFFFF;
						border: none;
						/* 确保按钮完全显示 */
						display: flex;
						align-items: center;
						justify-content: center;
						/* 防止按钮被挤压 */
						flex-shrink: 0;
						/* 设置边框盒模型，确保边框不占用额外空间 */
						box-sizing: border-box;
						/* 动画效果 */
						transition: all 0.2s ease;
						box-shadow: 0 4rpx 12rpx rgba(255, 124, 0, 0.3);

						&:active {
							background: linear-gradient(90deg, #FF6B00, #FF8400);
							box-shadow: 0 2rpx 8rpx rgba(255, 124, 0, 0.4);
						}
					}
				}

				.add-btn-info {
					width: 100%;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					background: linear-gradient(90deg, #FF7C00, #FF9500);
					border-radius: 8rpx;
					font-size: 28rpx;
					font-weight: 500;
					color: #FFFFFF;
					/* 确保按钮完全显示 */
					display: flex;
					align-items: center;
					justify-content: center;
					/* 防止按钮被挤压 */
					flex-shrink: 0;
					/* 添加底部边距，确保按钮与底部有适当距离 */
					margin-bottom: 10rpx;
					/* 设置边框盒模型，确保边框不占用额外空间 */
					box-sizing: border-box;
					/* 为商品详情页的加入购物车按钮添加上边距 */
					margin-top: 10rpx;
					/* 动画效果 */
					transition: all 0.2s ease;
					box-shadow: 0 4rpx 12rpx rgba(255, 124, 0, 0.3);

					&:active {
						background: linear-gradient(90deg, #FF6B00, #FF8400);
						box-shadow: 0 2rpx 8rpx rgba(255, 124, 0, 0.4);
					}
				}
				
				/* 分类页的加入购物车按钮特殊样式 */
				.add-btn-info.category-page {
					margin: 0 auto;
					margin-top: 0;
					border-radius: 8rpx;
				}
				


				.score-btn {
					width: 100%;
					height: 80rpx;
					line-height: 80rpx;
					text-align: center;
					background: linear-gradient(90deg, #FF7C00, #FF9500);
					border-radius: 8rpx;
					font-size: 28rpx;
					font-weight: 500;
					color: #FFFFFF;
					/* 确保按钮完全显示 */
					display: flex;
					align-items: center;
					justify-content: center;
					/* 防止按钮被挤压 */
					flex-shrink: 0;
					/* 添加底部边距，确保按钮与底部有适当距离 */
					margin-bottom: 10rpx;
					/* 设置边框盒模型，确保边框不占用额外空间 */
					box-sizing: border-box;
					/* 动画效果 */
					transition: all 0.2s ease;

					&:active {
						background: linear-gradient(90deg, #FF6B00, #FF8400);
					}
				}
			}
		}
	}
	
	/* 针对小屏幕设备的特殊优化 */
	@media screen and (max-height: 700px) {
		.ss-modal-box {
			max-height: calc(99vh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
		}

		.modal-content {
			max-height: calc(60vh - env(safe-area-inset-bottom));
		}

		.modal-content-scroll {
			max-height: calc(55vh - env(safe-area-inset-bottom));
		}

		.modal-footer {
			min-height: 120rpx;
			padding: 15rpx 20rpx;
		}

		.buy-box {
			min-height: 100rpx;
		}
	}

	/* 针对苹果设备的特殊优化 */
	@supports (-webkit-touch-callout: none) {
		.ss-modal-box {
			/* 苹果设备上的高度优化 */
			max-height: calc(100vh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
			/* 确保在苹果设备上完全显示，不被遮挡 */
			z-index: 1000000 !important;
			position: relative;
			/* 苹果设备硬件加速 */
			transform: translateZ(0);
			-webkit-transform: translateZ(0);
			/* 确保弹框在苹果设备上完全显示 */
			width: 100% !important;
		}

		.modal-content {
			max-height: calc(40vh - env(safe-area-inset-bottom));
		}

		.modal-content-scroll {
			max-height: calc(35vh - env(safe-area-inset-bottom));
		}

		.modal-footer {
			min-height: 120rpx;
			/* 确保底部按钮在苹果设备上完全显示 */
			position: relative;
			z-index: 1000001;
		}

		.buy-box {
			min-height: 100rpx;
		}

		/* 确保弹框在苹果设备上不被左侧菜单遮挡 */
		.modal-header {
			z-index: 1000001;
		}

		.close-btn {
			z-index: 1000002 !important;
		}
	}
</style>